//
//  PywUnbundAlertView.m
//  PywSdk
//
//  Created by smallwave on 16/9/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUnbundAlertView.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"
#import "PywAppInfo.h"
#import "PywFileManager.h"
#import "AppUtil.h"
#import "PywBuriedManager.h"

@interface PywUnbundAlertView ()
@property (nonatomic,strong) UIView         *mainView;
@property (nonatomic,strong) UILabel        *titleLable;
@property (nonatomic,strong) UIButton       *closeButton;
@property (nonatomic,strong) UIImageView    *iconImgView;
@property (nonatomic,strong) UILabel        *messageLabel;
@property (nonatomic,strong) UIButton       *sureButton;

@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *messageImg;
@property (nonatomic,copy) NSString *message;
@property (nonatomic,copy) NSString *sureBtnTitle;

@end

@implementation PywUnbundAlertView

- (instancetype)initWithTitle:(NSString *)title
                      message:(NSString *)message
                 messageImage:(NSString *)messageImage
                     delegate:(id<PywUnbundAlertViewDelegate>)delegate
              sureButtonTitle:(NSString *)sureButtonTitle
{
    self = [super init];
    if (self) {
        self.title = title;
        self.messageImg = messageImage;
        self.message = message;
        self.sureBtnTitle = sureButtonTitle;
        self.delegate = delegate;
        [self setUp];
    }
    return self;
}
-(void)dealloc
{
    if ([self.title isEqualToString:@"未绑定手机"]) {
        [PywBuriedManager manager].page_sn = @"";
//        [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"3" actionSn:@"2" typeSn:@"2"];
    }
}
#pragma mark - setUp
- (void)setUp
{
    self.backgroundColor = [UIColor colorWithWhite:0. alpha:.6];
    if ([self.title isEqualToString:@"未绑定手机"]){
        [PywBuriedManager manager].page_sn = @"3";
        [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"3" actionSn:@"1" typeSn:@"1"];
    }
    [self addSubview:self.mainView];
    [self.mainView addSubview:self.titleLable];
    [self.mainView addSubview:self.closeButton];
    [self.mainView addSubview:self.iconImgView];
    [self.mainView addSubview:self.messageLabel];
    [self.mainView addSubview:self.sureButton];
    [self addConstraints];
}

/**
 *  添加约束
 */
- (void)addConstraints
{
    __weak typeof(self) weakSelf = self;
    [self.mainView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.center.offset(0);
        if (IS_IPHONE) {
            make.width.equalTo(@(SCREEN_MIN_LENGTH * 0.9));
        } else {
            make.width.equalTo(@370);
        }
    }];
    
    [self.titleLable mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.mainView);
        make.height.mas_equalTo(40.f);
    }];
    
    [self.closeButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titleLable.mas_centerY);
        make.right.equalTo(weakSelf.mainView.mas_right).offset(-10);
    }];
    
    [self.iconImgView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titleLable.mas_bottom).offset(20);
        if (IS_IPHONE && !IS_IPHONE_6P) {
            make.size.mas_equalTo(CGSizeMake(60.f, 60.f));
            make.left.equalTo(weakSelf.sureButton.mas_left).offset(10);
        } else {
            make.size.mas_equalTo(CGSizeMake(80.f, 80.f));
            make.left.equalTo(weakSelf.sureButton.mas_left).offset(30);
        }
    }];
    
    [self.messageLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImgView.mas_right);
        make.centerY.equalTo(weakSelf.iconImgView.mas_centerY);
        if (IS_IPHONE && !IS_IPHONE_6P) {
            make.right.equalTo(weakSelf.sureButton.mas_right).offset(-10);
        } else {
            make.right.equalTo(weakSelf.sureButton.mas_right).offset(-20);
        }
    }];
    
    [self.sureButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.iconImgView.mas_bottom).offset(20);
        make.width.equalTo(weakSelf.mainView.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
        make.bottom.equalTo(weakSelf.mainView.mas_bottom).offset(-40);
    }];
}
#pragma mark - Action
/**
 *  点击按钮
 *
 *  @param sender <#sender description#>
 */
- (void)selectButton:(UIButton *)sender
{
    if ([self.delegate respondsToSelector:@selector(bundAlertView:clickedButtonAtIndex:)]) {
        [self.delegate bundAlertView:self clickedButtonAtIndex:sender.tag];
    }
    [self hide];
}
+ (BOOL)isOpenDialog
{
    if ([PywAppInfo sharedAppInfo].dialog_time == 0) {
        return NO;
    }
    if (![[PywFileManager manager] objectForKey:@"lastOpenDialogTime"]) {
        return YES;
    }
    
    long lastOpenTime = [[[PywFileManager manager] objectForKey:@"lastOpenDialogTime"] longValue];
    long dialog_time = [PywAppInfo sharedAppInfo].dialog_time * 60;
    
    return [AppUtil timeUp:lastOpenTime differentTimeStamp:dialog_time];
}
/**
 *  显示提示框
 */
- (void)show
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.right.bottom.offset(0);
    }];
    
}
/**
 *  隐藏提示框
 */
- (void)hide
{
    [self removeFromSuperview];
}

#pragma mark - Lazy
- (UIView *)mainView
{
    if (!_mainView) {
        _mainView = [[UIView alloc] init];
        _mainView.backgroundColor = [UIColor whiteColor];
        _mainView.layer.cornerRadius = 20.f;
        _mainView.layer.masksToBounds = YES;
    }
    return _mainView;
}
- (UILabel *)titleLable
{
    if (!_titleLable) {
        _titleLable = [UILabel new];
        _titleLable.font = [UIFont systemFontOfSize:16];
        _titleLable.textAlignment = NSTextAlignmentCenter;
        _titleLable.text = self.title;
        _titleLable.backgroundColor = [UIColor colorWithRed:250/255.f
                                                        green:250/255.f
                                                         blue:250/255.f
                                                        alpha:1.f];
        [_titleLable setAccessibilityIdentifier:@"alert_unbin_title"];
    }
    return _titleLable;
}
- (UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] initWithImage:
                        [UIImage imageWithName:self.messageImg]];
    }
    return _iconImgView;
}
- (UILabel *)messageLabel
{
    if (!_messageLabel) {
        _messageLabel = [UILabel new];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.numberOfLines = 2;
        
        // 文字自适应Label宽度，adjustsFontSizeToFitWidth 和 baselineAdjustment 需要配合使用，否则不会居中显示
        _messageLabel.adjustsFontSizeToFitWidth = YES;
        _messageLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        
        NSString *infoMessage = self.message;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:infoMessage];
        NSMutableParagraphStyle *parahraphStyle = [[NSMutableParagraphStyle alloc] init];
        parahraphStyle.alignment = NSTextAlignmentCenter;
        [parahraphStyle setLineSpacing:10]; // 设置行间距
        [attributedString addAttribute:NSParagraphStyleAttributeName
                                 value:parahraphStyle
                                 range:NSMakeRange(0, infoMessage.length)];
        _messageLabel.attributedText = attributedString;
        
        [_messageLabel sizeToFit];
    }
    return _messageLabel;
}
- (UIButton *)closeButton{
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeButton.tag = 0;
        _closeButton.backgroundColor = [UIColor clearColor];
        [_closeButton setBackgroundImage:[UIImage imageWithName:@"icon_close_normal"] forState:UIControlStateNormal];
        [_closeButton addTarget:self
                        action:@selector(selectButton:)
              forControlEvents:UIControlEventTouchUpInside];
        [_closeButton setAccessibilityIdentifier:@"alert_unbin_close"];
    }
    return _closeButton;
}
- (UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureButton.tag = 1;
        _sureButton.layer.cornerRadius = 20;
        _sureButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [_sureButton setTitle:self.sureBtnTitle
                     forState:UIControlStateNormal];
        _sureButton.backgroundColor = [UIColor colorWithRed:255/255.f
                                                      green:188/255.f
                                                       blue:0/255.f
                                                      alpha:1.f];
        [_sureButton setTitleColor:[UIColor blackColor]
                          forState:UIControlStateHighlighted];
        [_sureButton setTitleColor:[UIColor whiteColor]
                          forState:UIControlStateNormal];
        [_sureButton addTarget:self
                        action:@selector(selectButton:)
              forControlEvents:UIControlEventTouchUpInside];
        [_sureButton setAccessibilityIdentifier:@"alert_unbin_sure"];
    }
    return _sureButton;
}

@end
